package com.rybbaby.tss.front.tssMemberChangeClassRecord.service.imp;

import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssClassesLessons.dao.ITssClassesLessonsDao;
import com.rybbaby.tss.front.tssClassesLessons.entity.TssClassesLessons;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.dao.ITssMemberAttendClassRecordDao;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.entity.TssMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberChangeClassRecord.dao.ITssMemberChangeClassRecordDao;
import com.rybbaby.tss.front.tssMemberChangeClassRecord.entity.TssMemberChangeClassRecord;
import com.rybbaby.tss.front.tssMemberChangeClassRecord.service.ITssMemberChangeClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.dao.ITssMemberCheckAttendanceDao;
import com.rybbaby.tss.front.tssMemberPackage.dao.ITssMemberPackageDao;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberReturnClassRecord.entity.TssMemberReturnClassRecord;
import com.rybbaby.tss.front.tssMemberReturnClassRecord.service.ITssMemberReturnClassRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 会员转班记录表数据库操作服务实现
 * @createDate 2017-08-24 下午2:44:30
 */
@Service
@Transactional
public class TssMemberChangeClassRecordServiceImp extends BaseServiceImp<TssMemberChangeClassRecord> implements ITssMemberChangeClassRecordService {

    @Autowired
    private ITssMemberAttendClassRecordService tssMemberAttendClassRecordService;
    @Autowired
    private ITssMemberPackageDao tssMemberPackageDao;
    @Autowired
    private ITssMemberChangeClassRecordDao dao;
    @Autowired
    private ITssClassesLessonsDao tssClassesLessonsDao;
    @Autowired
    private ITssMemberAttendClassRecordDao attendClassRecordDao;
    @Autowired
    private ITssMemberCheckAttendanceDao checkAttendanceDao;
    @Autowired
    private ITssMemberReturnClassRecordService returnClassRecordService;

    @Override
    public boolean saveEntity(TssMemberChangeClassRecord entity, String targetdataId, String memberPackageId, String classId,String lessonIds,String adminUserId) throws Exception {
        boolean bool = true;
        String lessonId = "";
        String attendIds = "";
        TssMemberPackage memberPackage = tssMemberPackageDao.get(TssMemberPackage.class, memberPackageId);
        //查询已报班未未考勤的课节id
        List<Map<String, Object>> listMap = attendClassRecordDao.getChangeClassLessons(targetdataId, memberPackageId);
        if (listMap != null) {
            lessonId = this.getString(listMap, "lessonsId", ",");
            attendIds = this.getString(listMap,"id","','");
        }
        //把原考勤进行逻辑删除
        Integer updateId = checkAttendanceDao.deleteCheckAttendanceRecord(attendIds);
        if (updateId <= 0) {
            throw new Exception("修改原考勤记录失败");
        }
        //选择新班级的课节进行报班
        TssMemberAttendClassRecord memberAttendClassRecord = new TssMemberAttendClassRecord();
        bool = returnClassRecordService.saveEntity(new TssMemberReturnClassRecord(),lessonId,memberPackageId,memberPackage.getMemberId(),adminUserId,false);
        bool = this.tssMemberAttendClassRecordService.saveEntity(memberAttendClassRecord, lessonIds, memberPackageId,true);
        if (!bool) {
            return !bool;
        }
        //添加转班记录
        entity.setMemberId(memberPackage.getMemberId());
        entity.setBeforeClassId(targetdataId);
        entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
        entity.setAfterClassId(classId);
        this.dao.save(entity);
        //把未上课的改为已转班
        updateId = this.tssMemberAttendClassRecordService.updateNoClassLessionChangealreadyClass(2, memberPackage.getMemberId(), memberPackageId, lessonId);
        if (updateId <= 0) {
            throw new Exception("修改原班级失败");
        }
        return bool;

    }

    /**
     * 将List<Map<String,Object>> 形式进行字符串拼接
     *
     * @param objListMap 集合
     * @param keyName    要进行分割的map key
     * @param split      以什么符号进行中间分割默认逗号分隔
     * @return
     */
    private String getString(List<Map<String, Object>> objListMap, String keyName, String split) {
        String keyValues = "";
        List<Object> list = new ArrayList<>();
        if (StringUtils.isBlank(split)) {
            split = ",";
        }
        objListMap.forEach(map -> {
            list.add(map.get(keyName));
        });
        if (list != null) {
            keyValues = StringUtils.join(list, split);
        }
        return keyValues;
    }
}

